<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:wmsWarehouseBase:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:wmsWarehouseBase:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:wmsWarehouseBase:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:wmsWarehouseBase:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsWarehouseBaseList" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="仓库类型" align="center" prop="warehouseType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_warehouse_type" :value="scope.row.warehouseType">
          </dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="仓库状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_warehouse_status" :value="scope.row.status">
          </dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="address" :show-overflow-tooltip='true' />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="联系邮箱" align="center" prop="contactEmail" />
      <el-table-column label="建筑面积(㎡)" align="center" prop="totalArea" />
      <el-table-column label="可用面积(㎡)" align="center" prop="usableArea" />
      <el-table-column label="仓库描述" align="center" prop="description" :show-overflow-tooltip='true' />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:wmsWarehouseBase:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:wmsWarehouseBase:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库基础信息对话框 -->
    <el-drawer
      :title=title
      :visible.sync="open"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
      size="48%"
    >
      <div class="demo-drawer__content">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="warehouseName">
              <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" style="width: 70%"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>

          <el-col :span="12">
            <el-form-item label="仓库类型" prop="warehouseType">
              <el-select v-model="form.warehouseType" placeholder="请选择仓库类型">
                <el-option v-for="dict in dict.type.wms_warehouse_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="仓库状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择仓库状态">
                <el-option v-for="dict in dict.type.wms_warehouse_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" style="width: 70%"/>
        </el-form-item>

        <el-row>

          <el-col :span="12">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人"  style="width: 70%"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系电话"  style="width: 70%"/>
            </el-form-item>
          </el-col>
        </el-row>



        <el-form-item label="联系邮箱" prop="contactEmail">
          <el-input v-model="form.contactEmail" placeholder="请输入联系邮箱" style="width: 70%"/>
        </el-form-item>


        <el-row>

          <el-col :span="12">
            <el-form-item label="建筑面积(㎡)" prop="totalArea" label-width="100px">
              <el-input v-model="form.totalArea" placeholder="请输入建筑面积(㎡)" style="width: 50%"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="可用面积(㎡)" prop="usableArea" label-width="100px">
              <el-input v-model="form.usableArea" placeholder="请输入可用面积(㎡)" style="width: 50%"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="仓库描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入仓库描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listWmsWarehouseBase, getWmsWarehouseBase, delWmsWarehouseBase, addWmsWarehouseBase, updateWmsWarehouseBase } from "@/api/system/wmsWarehouseBase"

export default {
  name: "WmsWarehouseBase",
  dicts: ['wms_warehouse_status','wms_warehouse_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库基础信息表格数据
      wmsWarehouseBaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 仓库描述时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseName: null,
        warehouseType: null,
        status: null,
        regionId: null,
        address: null,
        contactPerson: null,
        contactPhone: null,
        contactEmail: null,
        totalArea: null,
        usableArea: null,
        description: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        warehouseType: [
          { required: true, message: "仓库类型:1-原材料仓,2-成品仓,3-半成品仓,4-退货仓,5-中转仓,6-其他不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "仓库状态:1-活跃,2-停用,3-维护中不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询仓库基础信息列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0]
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1]
      }
      listWmsWarehouseBase(this.queryParams).then(response => {
        this.wmsWarehouseBaseList = response.rows
        this.total = response.total
        this.loading = false
      })

    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        warehouseId: null,
        warehouseName: null,
        warehouseType: null,
        status: null,
        regionId: null,
        address: null,
        contactPerson: null,
        contactPhone: null,
        contactEmail: null,
        totalArea: null,
        usableArea: null,
        description: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warehouseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加仓库基础信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const warehouseId = row.warehouseId || this.ids
      getWmsWarehouseBase(warehouseId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改仓库基础信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warehouseId != null) {
            updateWmsWarehouseBase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addWmsWarehouseBase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warehouseIds = row.warehouseId || this.ids
      this.$modal.confirm('是否确认删除仓库基础信息编号为"' + warehouseIds + '"的数据项？').then(function() {
        return delWmsWarehouseBase(warehouseIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wmsWarehouseBase/export', {
        ...this.queryParams
      }, `wmsWarehouseBase_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
